package com.ms.oschina.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.View;

import com.ms.oschina.R;
import com.ms.oschina.api.remote.OsChinaApi;
import com.ms.oschina.base.LoadingPager.ResultCode;
import com.ms.oschina.bean.Constants;
import com.ms.oschina.bean.PostDetail;
import com.ms.oschina.util.HTMLUtil;
import com.ms.oschina.util.UiUtils;
import com.ms.oschina.viewholder.EventDetailTitleViewHoder;

public class EventDetailsActivity extends WebpageDetailsActivity {
	private PostDetail activitiesDetail;
	private EventDetailTitleViewHoder vh;

	protected ResultCode initNetData() {
		try {
			Intent intent = getIntent();
			// 区分是哪个页面请求的数据
			int id = intent.getIntExtra(Constants.EVENTDETAILS_ID, 0);
			activitiesDetail = OsChinaApi.getActivitiesDetail(id);
			return ResultCode.STATUS_SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ResultCode.STATUS_ERRORS;
		}
	}

	@Override
	protected CharSequence getBackActionBarTitle() {
		return UiUtils.getString(R.string.actionbar_title_event_detail);
	}

	@Override
	public void onSuccessViewData() {
		setTitle();
		StringBuffer body = new StringBuffer();
		String content = activitiesDetail.getPost().getBody();

		// 为html添加js代码可以再内容中打开图片查看图片大图
		body.append(HTMLUtil.setHtmlCotentSupportImagePreview(content));

		getWvDetailsContent().loadDataWithBaseURL("", body.toString(),
				"text/html", "utf-8", "");
	}

	private void setTitle() {
		vh.setData(0, activitiesDetail);
	}

	@Override
	protected void processClick(View v) {
		int id = v.getId();
		if (id == R.id.bt_eventdetail_attend) {

		} else if (id == R.id.bt_eventdetail_apply) {// 报名
			// 意图
			Intent intent = new Intent();
			// 意图的行为，隐式意图
			intent.setAction(Intent.ACTION_VIEW);
			// 意图的数据
			intent.setData(Uri.parse(activitiesDetail.getPost().getUrl()));
			startActivity(intent);
		} else if (id == R.id.rl_event_location) {
// 一会接入百度地图
			Context context = UiUtils.getContext();
			Intent intent = new Intent(context, MapActivity.class);
			String location = activitiesDetail.getPost().getEvent()
					.getLocation();

			intent.putExtra(Constants.LOCATION, location);// 精度
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			context.startActivity(intent);
		}

	}

	@Override
	public View getTitleView() {
		vh = new EventDetailTitleViewHoder();
		vh.setOnClickListener(this);
		return vh.getRootView();
	}
}
